import { put, call, take } from 'redux-saga/effects'
import { registing, registerSuccess, registerFail } from '../actions/register'
import * as types from '../constants/ActionTypes'
import { request } from '../utils/RequestUtils'


export function* requestRegister(uname, pwd, phone) {
  yield put(registing(uname, pwd, phone));
  const response = yield call(request, 'http://45.32.251.71/login.php',
   `username=${uname}&password=${pwd}&phone=${phone}`);
   if (response.status === 'success') {
     yield put(registerSuccess(uname, pwd));
   }
   else {
     yield put(registerFail('fail'))
   }
}

export function* watchRequestRegister() {
  while (true) {
    const {
      uname,
      pwd,
      phone,
    } = yield take(types.REGISTER);
    yield call(requestRegister, uname, pwd, phone);
  }
}